package com.ynr.bank.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;

import com.ynr.bank.service.IBankService;

@Controller
public class BankController {
	
	private static final Logger logger = LoggerFactory.getLogger(BankController.class);
	
	@Autowired
	private IBankService bankService;
	
	@RequestMapping("/uploadBankStatement")
	@ResponseBody
	public Map<String, Object> uploadBankStatement(DefaultMultipartHttpServletRequest multipartRequest,
			HttpServletRequest request) {
		logger.debug("entrance uploadBankStatement");
		List<MultipartFile> multipartFileList = multipartRequest.getFiles("files");
		String billDate = request.getParameter("bill_date");
		int companyId = Integer.valueOf(request.getParameter("company_id"));
		String bankType = request.getParameter("bank_type");
		String rootPath = "/home/files/bank";
		if (!multipartFileList.isEmpty()) {
			for(MultipartFile multifile : multipartFileList) {
				bankService.uploadBankStatement(billDate, companyId, rootPath, multifile, bankType);
			}
		}
		Map<String, Object> retMap = new HashMap<>();
		retMap.put("url", "");
		return retMap;
	}
	
}
